//
//  CCYUserAccount.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/22.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit

class CCYUserAccount: NSObject,NSCoding {
     /*
     "access_token" = "2.00nqBeqBQNMLTCac107e59eeVSK3PC";
     "expires_in" = 157679999;
     "remind_in" = 157679999;
     uid = 1694042541;
     */
    /// 用户授权的唯一票据
    var access_token:String?
    /// access_token的生命周期，单位是秒数。
    var expires_in: TimeInterval = 0 {
        didSet {
        expiresDate = Date(timeIntervalSinceNow: expires_in)
        }
    }
    /// 授权用户的UID
    var uid: String?
    
    ///  过期时间
    var expiresDate:Date?
    
    /// 用户昵称
    var screen_name: String?
    
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    
    init(dict:[String: Any]) {
        super.init()
        
        self.setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        print("没有找到的key\(key)")
    }
    
    override var description: String{
        get {
            let des = "access_token = \(access_token), expires_in = \(expires_in), uid = \(uid), expiresDate = \(expiresDate)"
            return des
        }
    }
    
    /// 解档对象调用
    required init(coder aDecoder:NSCoder){
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_in = aDecoder.decodeDouble(forKey: "expires_in")
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        expiresDate = aDecoder.decodeObject(forKey: "expriesDate") as? Date
        screen_name = aDecoder.decodeObject(forKey: "screen_name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
    
    /// 归档对象调用
    func encode(with aCorder:NSCoder){
        aCorder.encode(access_token, forKey: "access_token")
        aCorder.encode(expires_in, forKey: "expires_in")
        aCorder.encode(uid, forKey: "uid")
        aCorder.encode(expiresDate, forKey: "expriesDate")
        aCorder.encode(screen_name, forKey: "screen_name")
        aCorder.encode(avatar_large, forKey: "avatar_large")
    }
    
}
